package com.gzp;

import java.io.UnsupportedEncodingException;

import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;

/**
 * zookeeper序列化工具类s
 * 
 * @author Administrator
 *
 */
public class CustomSerializer implements ZkSerializer {

	private String charset = "UTF-8";

	public CustomSerializer() {
	}

	public CustomSerializer(String charset) {
		this.charset = charset;
	}

	/**
	 * 序列化
	 */
	public byte[] serialize(Object data) throws ZkMarshallingError {
		try {
			byte[] bytes = String.valueOf(data).getBytes(charset);
			return bytes;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 反序列化
	 */
	public Object deserialize(byte[] bytes) throws ZkMarshallingError {
		String result = null;
		try {
			result = new String(bytes, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

}
